package org.example.springmvc1;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonText {
    public static void main(String[] args) throws JsonProcessingException {


        ObjectMapper mapper = new ObjectMapper();

        User user = new User();
        user.setName("hx");
        user.setAge(18);
        user.setGender("boy");

        //使用这个来将 Java 对象转换成 Json 字符串
        String jsonUser = mapper.writeValueAsString(user);

        //打印 Json 文档
        System.out.println(jsonUser);

        //将 Json 字符串转换成 Java 对象
        User u = mapper.readValue(jsonUser, User.class);//前面的参数表示要转换的对象,后面的参数表示要转换成的对象的类

        System.out.println("name:" +u.getName() + "\ngender:" + u.getGender() + "\nage:" + u.getAge());
    }
}
